﻿using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace Zeus.Mvc.Middlewares
{
    using Resources;
    public class HttpCorrelationIdMiddleware
    {
        private RequestDelegate _next;

        public HttpCorrelationIdMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            if (!context.Request.Headers.ContainsKey(ResourcesHttpHeader.HttpCorrelationId))
            {
                context.Request.Headers.Add(ResourcesHttpHeader.HttpCorrelationId, Guid.NewGuid().ToString("N"));
            }
            await _next(context);
        }
    }
}
